<?
require("../../common/init.php");
require("author.php");
require("upfile.php");
include(ROOT."common/class_image_resize.php");

$refund_reason = new DB(Trefund_reason, $conn);
$customer = new DB(Tcustomer, $conn);
$order_service = new DB(Torder_service, $conn);
$gee=$order_service; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
$sales_order_detail=new DB(Tsales_order_detail,$conn);
//$cate=$product_category; // cate = category, 分类

$filter="1";
if(isset($_GET["filter"]))$filter=clean($_GET["filter"]);
if(isset($_POST["filter"]))$filter=clean($_POST["filter"]);

switch ($filter){
    case 1:
        $title=ROOT_ORDER_REFUND_CHECKING;
        break;
    case 2:
        $title=ROOT_ORDER_REFUND_NO_CHECKED;
        break;
    case 3:
        $title=ROOT_ORDER_REFUND_REFUND;
        break;
    case 4:
        $title=ROOT_ORDER_REFUND_REFUNDED;
        break;
    default:
        $title=ROOT_ORDER_REFUND_CHECKED;
}
$url='order_service';
$s=isset($_GET["s"])?clean($_GET["s"]):''; // search
$cateid=isset($_GET["cid"])?clean($_GET["cid"]):0;
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"]; // current page 当前页码
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);

// sql action / sql 动作
switch ($action){
    case "update":
        sql_update();
        break;
    case "add":
        sql_insert();
        break;
}

if($id==0){
    $order='';
    $gee->rows=15;
    $where=array();
    if($filter!=''){
        $where[]=array('name'=>'refund_status','val'=>$filter,'opn'=>'=', 'and'=>'and');
    }
    if($s!='') {
        $where[]=array('name'=>'sales_order_id','val'=>$s,'opn'=>'like', 'and'=>'and');
        $where[]=array('name'=>'contact_name','val'=>$s,'opn'=>'like', 'and'=>'and');
        $where[]=array('name'=>'product_name','val'=>$s,'opn'=>'like', 'and'=>'and');
        $where[]=array('name'=>'phone','val'=>$s,'opn'=>'like', 'and'=>'and');
    }
    $pages=$gee->count_pages($where);
    $info=$gee->get_page($page,$where);
}
else $info=$gee->get_row($id);

function update($info)
{
    global $gee, $url, $h, $id;
    if($id==0)
    {
        echo "Invali argument!!!";
        exit;
    }
    echo $h->form($url.'.php', 1, '');
    show_box($info,true, false);
    echo $h->hidden('action', 'update');
    echo $h->former();
}
function delete($id)
{
    global $gee,$url;
    if($gee->delete($id)) wheel($url.'.php');
}
function show_info()
{
    global $gee,$info,$id;
    if($id==0) show_boxes($info);
    else show_box($info, false, false);
}
function sql_update()
{
    global $product, $gee, $url, $filter, $customer, $page, $template, $recode;

    $id=iso(@$_POST["id"]);
    $info_refund=$gee->get_row($id); // refund/订单取消
    $candelete=0;
    $arr=array(
//        'shipped_time'=>iso(@$_POST["shipped_time"]),
//        'express'=>iso(@$_POST["express"]),
//        'shipment_tracking_no'=>iso(@$_POST["shipment_tracking_no"]),
        'refund_status'=>iso(@$_POST["refund_status"]),
        'operator_remark'=>iso(@$_POST["operator_remark"]),
        'update_time' => date('Y-m-d H:i:s', time()),
        'update_user_id' => decrypt($_COOKIE['userid']),
    );

    if($gee->update($arr, 'id', $id)){
        if($_POST['refund_status'] == 3){
            $where_r = array();
            $where_r[] = array('name'=>'template_id', 'val'=>1, 'opn'=>'=', 'and'=>'and');
            $where_r[] = array('name'=>'recode_name', 'val'=>'sales_order_id', 'opn'=>'=', 'and'=>'and');
            $where_r[] = array('name'=>'recode_value', 'val'=>$_POST['sales_order_id'], 'opn'=>'=', 'and'=>'and');
            $r = $recode->get_rows_advanced(1, $where_r);
            if(!$r){
                $wallet_arr = array(
                    'template_id' => 1,
                    'value' => $_POST['sales_order_id'],
                    'type' => 1,
                    'customer_id' => $_POST['customer_id'],
                    'user_id' => $_POST['userid'],
                    'create_time' => date('Y-m-d H:i:s', time()),
                    'recode_name' => 'sales_order_id',
                    'recode_value' => $_POST['sales_order_id']
                );
                $recode->insert($wallet_arr);
            }else{
                $wallet_arr = array('create_time'=>date('Y-m-d H:i:s', time()));
                $recode->update($wallet_arr, 'id', $r[0]['id']);
            }
        }
        if($_POST['refund_status'] == 4){
            $where_r = array();
            $where_r[] = array('name'=>'template_id', 'val'=>2, 'opn'=>'=', 'and'=>'and');
            $where_r[] = array('name'=>'recode_name', 'val'=>'sales_order_id', 'opn'=>'=', 'and'=>'and');
            $where_r[] = array('name'=>'recode_value', 'val'=>$_POST['sales_order_id'], 'opn'=>'=', 'and'=>'and');
            $r = $recode->get_rows_advanced(1, $where_r);
            $customer_info = $customer->get_row($_POST['customer_id']);
            if(!$r){
                $wallet_arr = array(
                    'template_id' => 2,
                    'value' => $_POST['sales_order_id'].'|'.$_POST['refund_reason'].'|'.$_POST['operator_remark'].'|'.$_POST['refund_amount'].'|'.$_POST['refund_account'],
                    'type' => 1,
                    'customer_id' => $_POST['customer_id'],
                    'user_id' => $_POST['userid'],
                    'create_time' => date('Y-m-d H:i:s', time()),
                    'recode_name' => 'sales_order_id',
                    'recode_value' => $_POST['sales_order_id']
                );
                $recode->insert($wallet_arr);
                $customer_arr = array(
                    'usable_balance'=>$customer_info['usable_balance']+$_POST['refund_amount'],
                    'all_balance'=>$customer_info['all_balance']+$_POST['refund_amount'],
                    );
                $customer->update($customer_arr,'id', $_POST['customer_id']);
            }else{
                $wallet_arr = array(
                    'value' => $_POST['sales_order_id'].'|'.$_POST['refund_reason'].'|'.$_POST['operator_remark'].'|'.$_POST['refund_amount'].'|'.$_POST['refund_account'],
                    'user_id' => $_POST['userid'],
                    'create_time' => date('Y-m-d H:i:s', time()),
                    'recode_name' => 'sales_order_id',
                    'recode_value' => $_POST['sales_order_id']
                );
                $recode->update($wallet_arr, 'id', $r[0]['id']);
                $a = explode('|', $r[0]['value']);
                $customer_arr = array(
                    'usable_balance'=>$customer_info['usable_balance']+($_POST['refund_amount'] - $a[3]),
                    'all_balance'=>$customer_info['all_balance']+($_POST['refund_amount'] - $a[3]),
                );
                $customer->update($customer_arr,'id', $_POST['customer_id']);
            }
        }
//        yell(ROOT_HANDLE_SUCCESS,$url.'.php'.(($filter!="")?"?page=".$page."&filter=".$filter:""));
    }
    else echo mysql_error();
}

function product_detail($detail_id){
    global $product, $sales_order, $sales_order_detail, $product_variation, $option_label, $option_value, $lang, $province, $county, $city;
    $product_detail = array();
    $order_detail = $sales_order_detail->get_row($detail_id);
    $product_detail['p_id'] = $order_detail['product_id'];
    $product_detail['v_id'] = $order_detail['product_variation_id'];
    $product_info = $product->get_row($order_detail['product_id']);
    $product_detail['is_virtual'] = $product_info['is_virtual'];
    $str_var = '';
    $str_var .= $product_info['name_'.$lang].' ';
    if($order_detail['product_variation_id']>0){
        $tem_var=$product_variation->get_row($order_detail['product_variation_id']); // var
        if($tem_var['size']) $str_var.='['.$option_value[$tem_var['size']].'] ';
        if($tem_var['color']) $str_var.='['.$option_value[$tem_var['color']].'] ';
        if($tem_var['other1']) $str_var.='['.$option_value[$tem_var['other1']].'] ';
        if($tem_var['other2']) $str_var.='['.$option_value[$tem_var['other2']].'] ';
    }
    $product_detail['product_name'] = $str_var;
    $product_detail['quantity'] = $order_detail['quantity'];
    $product_detail['order_id'] = $order_detail['sales_order_id'];
    $sales_order_info = $sales_order->get_row($order_detail['sales_order_id']);
    $product_detail['sales_order_id'] = $sales_order_info['ref_no'];
    $tem_province=$province->get_name($sales_order_info['province'],$lang);
    $tem_addr=$tem_province;
    if(! empty($sales_order_info['city'])) { $tem_city=$city->get_name($sales_order_info['city'],$lang); $tem_addr.=$tem_city.'/'; }
    if(! empty($sales_order_info['county'])) { $tem_county=$county->get_name($sales_order_info['county'],$lang); $tem_addr.=$tem_county.'/'; }
    $tem_addr.=' '.$sales_order_info['address'];
    $product_detail['address'] = $tem_addr;
    $product_detail['phone'] = $sales_order_info['mobile'];
    $product_detail['contact_name'] = $sales_order_info['contact_name'];
    $product_detail['zip'] = $sales_order_info['zip'];

    return $product_detail;

}
// view 加载视图
require ROOTVIEW.$url.'.php';

// end sales_order.php
